package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.webmaster.client.CacheClient;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.CustomerMapper;
import com.qianfeng.openplatform.webmaster.pojo.Customer;
import com.qianfeng.openplatform.webmaster.pojo.CustomerExample;
import com.qianfeng.openplatform.webmaster.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/22 17:43
 */
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CacheClient cacheClient;

    @Override
    public LayuiDataTableSource getCusetomerPage(Integer page, Integer limit, String name, Integer state) {
        PageHelper.startPage(page, limit);
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria c = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            c.andNicknameLike(name);
        }
        if (state.intValue() != -1) {
            c.andStateEqualTo(state.shortValue());
        }
        List<Customer> customerList = customerMapper.selectByExampleWithBLOBs(example);
        PageInfo<Customer> info = new PageInfo<>(customerList);
        LayuiDataTableSource tableData = new LayuiDataTableSource();
        tableData.setCount(info.getTotal());
        tableData.setData(info.getList());
        return tableData;
    }

    @Override
    public OpenApiMaster addCustomer(Customer customer) {
        int code = customerMapper.insert(customer);
        if (code == 1) {
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("插入失败,请检查数据是否正确.......");
    }

    @Override
    public OpenApiMaster updateCustomer(Customer customer) {

        int i = customerMapper.updateByPrimaryKeySelective(customer);
        if (i == 1) {
            try {
            if (customer.getState()==1){
                cacheClient.hMset(RedisKeyPrefix.WEB_MASTER_API_CUSTOMER_KEY+customer.getId(),customer);
            }else {

                    cacheClient.deleteKey(RedisKeyPrefix.WEB_MASTER_API_CUSTOMER_KEY+customer.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("插入失败,请检查数据是否正确.......");
    }

    @Override
    public OpenApiMaster delCustomer(String[] arr) {
        int sum = 0;
        for (String s : arr) {

            int i = Integer.valueOf(s);
            int ii = customerMapper.deleteByPrimaryKey(i);
            sum = sum + ii;
        }
        System.out.println(sum);
        System.out.println(arr.length);
        if (arr.length == sum) {
            return OpenApiMaster.ok();
        } else {
            return OpenApiMaster.error("删除失败,请检查数据是否正确.......");
        }
    }

    @Override
    public List<Customer> getCustomerAll() {
        CustomerExample example = new CustomerExample();
        List<Customer> customerList = customerMapper.selectByExample(example);
        return customerList;
    }

    @Override
    public Customer getCustomerById(Integer customerId) {
        return customerMapper.selectByPrimaryKey(customerId);
    }
}
